<template>
  <div class="app">
    <div class="search-bar">
      <el-form :model="searchData" label-width="70px">
        <el-row :gutter="5">
          <el-col :span="5">
            <el-form-item label="计算日期" style="margin-bottom: 5px;">
              <el-date-picker clearable class="select-item" v-model="searchData.calculateDate" type="date" format="yyyy 年 MM 月 dd 日" size="medium" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="计算频率" style="margin-bottom: 5px;">
              <el-select v-model="searchData.calculateFreq" size="medium" class="select-item" clearable>
                <el-option
                  v-for="item in computeOption"
                  :key="item.index"
                  :label="item.label"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="指标ID" style="margin-bottom: 5px;">
              <el-input v-model="searchData.indexId" size="medium" clearable class="select-item"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="公司" style="margin-bottom: 5px;">
              <el-select
                class="select-item"
                size="medium"
                v-model="searchData.cmhkId"
                filterable
                remote
                reserve-keyword
                clearable
                placeholder="请输入公司名查询"
                :remote-method="remoteMethod">
                <el-option
                  v-for="item in companyOption"
                  :key="item.code"
                  :label="item.value"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <div style="margin-top: 3px; display: flex; justify-content: center">
              <el-button icon="el-icon-search" type="primary" size="small" @click="handleSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" size="small" @click="handleClear">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div ref="ref" class="table-box">
      <TableComponent :columns="tableColumns" :height="height" :data="tableData" @change="onTableChange" :tableOption="tableOption" :page-option="searchData.page" :stripe="stripe" :total="total">
      </TableComponent>
    </div>
  </div>
</template>

<script>
import TableComponent from '@/components/Table/table'
import { queryTargetData } from '@/api/modules/warning.js'
import { tableColumns, computeOption } from './index.js'
import { proxyShareholder } from '@/api/modules/propertyRight.js'
export default {
  components: {
    TableComponent
  },
  data() {
    return {
      searchData: {
        calculateDate: '',
        calculateFreq: '',
        indexId: '',
        cmhkId: '',
        page: {
          pageNum: 1,
          pageSize: 10
        }
      },
      tableColumns,
      computeOption,
      tableData: [],
      height: null,
      title: '',
      total: 0,
      tableOption: {
        isShowOperate: false
      },
      stripe: true,
      companyOption: []
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.height = this.$refs.ref.offsetHeight
    })
    window.onresize = () => {
      this.height = this.$refs.ref.offsetHeight
    }
    this.initData(this.searchData)
  },
  destroyed(){
    window.onresize = null
  },
  methods: {
    initData(params) {
      queryTargetData(params).then(res => {
        if (res.data.code === '200') {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        }
      })
    },
    // 点击查询
    handleSearch() {
      this.initData(this.searchData)
    },
    // 分页
    onTableChange(val) {
      this.searchData.page = val
      this.initData(this.searchData)
    },
    handleClear() {
      this.searchData.calculateDate = ''
      this.searchData.calculateFreq = ''
      this.searchData.indexId = ''
      this.searchData.cmhkId = ''
    },
    remoteMethod(query) {
      if (query !== '') {
        const data = {
          companyChiName: query
        }
        proxyShareholder(data).then(res => {
          this.companyOption = res.data.body
        })
      } else {
        this.companyOption = []
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .app {
    height: 100%;
    .search-bar {
      width: calc(100% - 60px);
      background-color: #ffffff;
      margin-bottom: 10px;
      padding: 10px 30px;
    }
    .add-form {
      padding: 10px 20px;
      background-color: #ffffff;
    }
    .table-box {
      width: 100%;
      height: calc(100vh - 200px);
      .table {
        height: 100%;
      }
    }
    .select-item {
      width: 100%;
    }
  }
</style>
